package stream;

import java.sql.Array;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author wang'ji'cheng
 * @date 2025/7/19
 */
public class Demo5 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"张三-男-23","李四-男-25","王五-男-26","张四-女-24","李武-女-16","李丽-女-25");
        //收集到list集合
        List<String> collect = list.stream().filter(s -> "男".equals(s.split("-")[1])).collect(Collectors.toList());
        System.out.println(collect);
        System.out.println("==============================");
        //收集到set集合
        Set<String> stringSet = list.stream().filter(s -> Integer.parseInt(s.split("-")[2]) >= 24 ).filter(s-> "女".equals(s.split("-")[1])).collect(Collectors.toSet());
        System.out.println(stringSet);


        System.out.println("=================================");
        //收集到map集合里
        Map<String, Integer> collect1 = list.stream().filter(s -> "男".equals(s.split("-")[1]))
                /*
                第一个参数new Function<String, String>()键的生成规则，泛型1是流中数据的类型，泛型2是变成map后值的类型，
                第二个同理，参数分别为流中数据类型和 值的数据类型
                apply方法，生成键 参数表示流中每一个数据的类型，返回值就是键
                 */
                .collect(Collectors.toMap(new Function<String, String>() {
                    @Override
                    public String apply(String s) {
                        return s.split("-")[0];
                    }
                }, new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) {
                        return Integer.parseInt(s.split("-")[2]);
                    }
                }));
        System.out.println(collect1);

        System.out.println("-------------------------------");
        Map<String, Integer> collect2 = list.stream()
                .filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toMap(s->s.split("-")[0],s->Integer.parseInt(s.split("-")[2])));
        System.out.println(collect2);
    }
}
